<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<br>
<%@ page import="org.atf.utils.*,org.atf.model.entity.User" %>
<%

	User user = null;
	try {
				
		user = (User)session.getAttribute("ATFUser");
		
	}catch (Exception e){
		response.sendRedirect("login.jsp");
	}
	if( user == null){
		response.sendRedirect("login.jsp");
	}
%>

<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist" id="dashboardTabs">
  <li class="active"><a href="#dashboardServer" role="tab" data-toggle="tab" >Server</a></li>
    <% if(user.getAdmin().equalsIgnoreCase("true")) { %>
  <!--  <li><a href="#dashboardWebsphere" role="tab" data-toggle="tab" onClick="uft.updateHrefDiv('dashboardWebsphere','jsp/wasDashboard.jsp');">Websphere</a></li>-->
  <li><a href="#dashboardHTTP" role="tab" data-toggle="tab" onClick="uft.updateHrefDiv('dashboardHTTP','jsp/apacheDashboard.jsp');">Apache HTTP</a></li>
  <li><a href="#dashboardDP" role="tab" data-toggle="tab" onClick="uft.updateHrefDiv('dashboardDP','jsp/dpDashboard.jsp');">Datapower</a></li>
  <%} %>
</ul>

<!-- Tab panes -->
<div class="tab-content">
  <div class="tab-pane active" id="dashboardServer">
  	<br>
  	<div class="row">
		<div class="col-md-2">
			 <div class="alert alert-info text-center" id="availableServersList">
	             <h4>Available Servers</h4>
	             <h1>0</h1>
	         </div>
		</div>
		<div class="col-md-2">
			 <div class="alert alert-success text-center" id="runningServersList">
	             <h4>Servers Running</h4>
	             <h1>0</h1>
	         </div>
		</div>
		<div class="col-md-2">
			 <div class="alert alert-danger text-center" id="stoppedServersList">
	             <h4>Servers Stopped</h4>
	             <h1>0</h1>
	         </div>
		</div>
		<div class="col-md-2">
			 <div class="alert alert-info text-center" id="currentTasksList">
	              <h4>Current Tasks</h4>
	               <h1>0</h1>
	         </div>
		</div>
		<% if(user.getAdmin().equalsIgnoreCase("true")) { %>
			<div class="col-md-2">
			 <div class="alert alert-info text-center" id="schedulerCountList">
	              <h4>Scheduled Tasks</h4>
	               <h1>0</h1>
	         </div>
			</div>
		<%} %>
	</div>
	<div class="row">
		<div class="col-lg-5">
			<div class="panel panel-primary">
				<div class="panel-heading">
			       <i class="fa fa-line-chart  fa-fw"></i> Sever Memory Usage
			    </div>
			     <div class="panel-body">
			    	 <div id="usedMemoryChart"></div>
			     </div>
			 </div>
			 
		</div>
		<% if(user.getAdmin().equalsIgnoreCase("true")) { %>
			<div class="col-lg-5">
				<div class="panel panel-primary">
					<div class="panel-heading">
				       <i class="fa fa-tasks  fa-fw"></i> Schedule Jobs
				    </div>
				     <div class="panel-body">
				    	<table class="table table-hover table-condensed" style="border: 0px" id="schedulerListTable">
				    		<thead><tr><th>Name</th><th>Start Time</th><th>Last Run</th><th>Next Run</th></tr></thead>
							<tbody id="schedulerListTableBody">
							</tbody>
						</table>
				     </div>
				 </div>
				 
			</div>
		<%} %>
	</div>
  </div>
  <div class="tab-pane" id="dashboardWebsphere" >
  <h4>loading.. </h4>
  </div>
  <div class="tab-pane" id="dashboardHTTP">
  	<h4>loading.. </h4>
  </div>
  <div class="tab-pane" id="dashboardDP">
  <h4>loading.. </h4>
  </div>
</div>

<script>
	$(document).ready(function() {
		var grapData = [];
	 	dashboardController.getJVMRuntimeStats({
		 		async:false,
				callback:function(data){
					grapData.push(data)
				}
		})
		var usedMemoryGrap = Morris.Area({
		    element: 'usedMemoryChart',
		    data: grapData,
		    xkey: 'genTimeStamp',
		    ykeys: ['usedMemory', 'totalMemory',],
		    labels: ['Used Memory', 'Total Memory',],
		    xLabels: ["1min"],
		    parseTime: true,
		    ymin: 0,
		    ymax: grapData[0].maxMemory,
		    hideHover: true,
		   
		 });
		 $("#availableServersList h1").text(uft.serverData.length)
		 $("#currentTasksList h1").text(uft.taskObjects.length)
		 <% if(user.getAdmin().equalsIgnoreCase("true")) { %>
		 	var schedulerJobs=	uft.getSchedulerJobsList()	
			$("#schedulerCountList h1").text(schedulerJobs.length);
			dwr.util.removeAllRows("schedulerListTableBody");
			dwr.util.addRows( "schedulerListTableBody",schedulerJobs,[
    				function(data) { return data.jobName; },
    				function(data) { return data.startTime; },
    				function(data) { return data.lastRun; },
    				function(data) { return data.nextRun; },
    				],{ 
			cellCreator:function(options) {
				 var row = document.createElement("td");
				 var cellNum=options.cellNum;
				 return row;
				},
			escapeHtml:false });		
		<%}%>
		 jQuery.each( uft.serverData, function( i, val ) {
			 if(val.status != null){
			 	if(val.status.toUpperCase() == "running".toUpperCase()|| val.status.toUpperCase() == "started".toUpperCase()){
			 		var n=$("#runningServersList h1").text()
			 		$("#runningServersList h1").text(parseInt(n)+1)
				 }
				 if(val.status.toUpperCase() == "stopped".toUpperCase()){
			 		var n=$("#stoppedServersList h1").text()
			 		$("#stoppedServersList h1").text(parseInt(n)+1)
				 }
				 
			 }
			 
		 	
		 })
		
	
		var updateMemory= function updateMemoryGraph(){
				 if($("#usedMemoryChart").visible()){
				 	dashboardController.getJVMRuntimeStats({
							callback:function(data){
								grapData.push(data)
								usedMemoryGrap.setData(grapData);
							}
						})
				 }else{
				 	uft.stopTimer("MD1")
				 }
					
			}
		uft.startTimer("MD1",updateMemory,10000);	
})
		
</script>
							